﻿<# 
.SYNOPSIS 
    Imports an existing PeopleSoft Application Server configuration.
.DESCRIPTION 
    The Import-PsftDomain cmdlet will take an existing psappsrv.cfg file
    and import it to create a new domain. The domain will be created with the default
    feature set. Use Set-PsftFeature to configure the features of this new domain.
.NOTES 
    File Name  : Import-PsftDomain.ps1 
    Author     : Scott Kearney - scott@kearney.tk 
    Requires   : PowerShell V3 
.EXAMPLE 
    Import-PsftDomain -PS_HOME C:\psoft\PS_HOME -PS_CFG_HOME C:\psoft\PS_CFG_HOME -Path C:\psoft\PS_CFG_HOME\appserv\DOMAIN1\psappsrv.cfg -NewDomain DOMAIN2
    Imports the domain configuration from DOMAIN1 and creates a new domain named DOMAIN2.
.PARAMETER PS_HOME
    The full path to the PS_HOME directory for this domain.
.PARAMETER PS_CFG_HOME
    The full path to the PS_CFG_HOME directory for this domain.
.PARAMETER Path
    The full path to the configuration file to import
.PARAMETER NewDomain
    The name of the domain to be created.
#> 
function Import-PsftDomain
{
    [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Medium')]
    Param
    (
        # The full path to the PS_HOME directory for this domain.
        # See http://docs.oracle.com/cd/E41633_01/pt853pbh1/eng/pt/tsvt/concept_UnderstandingPS_HOMEandPS_CFG_HOME-eb7ece.html
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The full path to the PS_HOME directory for this domain.",
                   Position=0)]
        [ValidateScript({Validate_PS_HOME $_})]
        [String]
        $PS_HOME,

        # The full path to the PS_CFG_HOME directory for this domain.
        # See http://docs.oracle.com/cd/E41633_01/pt853pbh1/eng/pt/tsvt/concept_UnderstandingPS_HOMEandPS_CFG_HOME-eb7ece.html
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The full path to the PS_CFG_HOME directory for this domain.",
                   Position=1)]
        [ValidateScript({Validate_PS_CFG_HOME $_})]
        [String]
        $PS_CFG_HOME,

        # The type of server on which to perform the action.
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The type of server on which to perform the action.",
                   Position=2)]
        [ValidateSet("AppServer","PrcsScheduler")]
        $ServerType,

        # The full path to the configuration file to import.
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=2)]
        [ValidateScript({Validate_Cfg_Path $_})]
        $Path,
        
        # The name of the domain to be created.
        # Will be a directory under the PS_CFG_HOME
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The name of the domain on which to perform the action.",
                   Position=3)]
        [ValidateScript({Validate_NewDomain $PS_CFG_HOME "AppServer" $_})]
        [String]
        $NewDomain
    )

    Begin
    {
    }
    Process
    {
        $return = @{}

        # Support WhatIf or Confirm by checking before importing the domain.
        if($PSCmdlet.ShouldProcess($NewDomain,$PSCmdlet.MyInvocation.InvocationName)){
            
            # Invokes PSAdmin.exe to import the configuration file and create the new domain.
            $PSAdmin = Invoke-PSAdmin -PS_HOME $PS_HOME -PS_CFG_HOME $PS_CFG_HOME -ServerType $ServerType -Domain $NewDomain -Command "import $Path" -ExtraCommands "-n $NewDomain"
            $return = $PSAdmin.Result
        }

        return $return
    }
    End
    {
    }
}